package com.dayu.web.controller.share;

import java.util.List;

import com.dayu.share.domain.ShareGoodsSpecsValues;
import com.dayu.share.service.IShareGoodsSpecsValuesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dayu.common.annotation.Log;
import com.dayu.common.core.controller.BaseController;
import com.dayu.common.core.domain.AjaxResult;
import com.dayu.common.enums.BusinessType;
import com.dayu.share.domain.ShareGoodsSpecs;
import com.dayu.share.service.IShareGoodsSpecsService;
import com.dayu.common.utils.poi.ExcelUtil;
import com.dayu.common.core.page.TableDataInfo;

/**
 * 规格管理Controller
 * 
 * @author HK
 * @date 2021-04-08
 */
@RestController
@RequestMapping("/share/specs")
public class ShareGoodsSpecsController extends BaseController
{
    @Autowired
    private IShareGoodsSpecsService shareGoodsSpecsService;
    @Autowired
    private IShareGoodsSpecsValuesService shareGoodsSpecsValuesService;

    /**
     * 查询规格管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ShareGoodsSpecs shareGoodsSpecs)
    {
        startPage();
        List<ShareGoodsSpecs> list = shareGoodsSpecsService.selectShareGoodsSpecsList(shareGoodsSpecs);
        return getDataTable(list);
    }

    /**
     * 导出规格管理列表
     */
    @PreAuthorize("@ss.hasPermi('share:specs:export')")
    @Log(title = "规格管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ShareGoodsSpecs shareGoodsSpecs)
    {
        List<ShareGoodsSpecs> list = shareGoodsSpecsService.selectShareGoodsSpecsList(shareGoodsSpecs);
        ExcelUtil<ShareGoodsSpecs> util = new ExcelUtil<ShareGoodsSpecs>(ShareGoodsSpecs.class);
        return util.exportExcel(list, "specs");
    }

    /**
     * 获取规格管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('share:specs:query')")
    @GetMapping(value = "/{specsId}")
    public AjaxResult getInfo(@PathVariable("specsId") Long specsId)
    {
        return AjaxResult.success(shareGoodsSpecsService.selectShareGoodsSpecsById(specsId));
    }

    /**
     * 新增规格管理
     */
    @PreAuthorize("@ss.hasPermi('share:specs:add')")
    @Log(title = "规格管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShareGoodsSpecs shareGoodsSpecs)
    {
        return toAjax(shareGoodsSpecsService.insertShareGoodsSpecs(shareGoodsSpecs));
    }

    /**
     * 修改规格管理
     */
    @PreAuthorize("@ss.hasPermi('share:specs:edit')")
    @Log(title = "规格管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShareGoodsSpecs shareGoodsSpecs)
    {
        return toAjax(shareGoodsSpecsService.updateShareGoodsSpecs(shareGoodsSpecs));
    }

    /**
     * 删除规格管理
     */
    @PreAuthorize("@ss.hasPermi('share:specs:remove')")
    @Log(title = "规格管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{specsIds}")
    public AjaxResult remove(@PathVariable Long[] specsIds)
    {
        for(Long id : specsIds) {
            if(id == 2) {
                return AjaxResult.error("不能删除系统默认规格！");
            }
            ShareGoodsSpecsValues specsValues = new ShareGoodsSpecsValues();
            specsValues.setSpecsId(id);
            List<ShareGoodsSpecsValues> values = shareGoodsSpecsValuesService.selectShareGoodsSpecsValuesList(specsValues);
            if(values.size() > 0) {
                return AjaxResult.error("规格下还有数据，不能删除！");
            }
        }
        return toAjax(shareGoodsSpecsService.deleteShareGoodsSpecsByIds(specsIds));
    }
}
